<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfcomponent>

	<cfset variables.instance = StructNew() />
	<cffunction name="init" access="public" output="false">
		<cfargument name="formFieldsLocation" default="e:\inetpub\www_ayc\testjjb_dat_dat.xml" required="true" />
		
		<cffile action="read" file="#arguments.formFieldsLocation#" variable="inStr" />
		<cfwddx action="wddx2cfml" input="#inStr#" output="formFields" />
		
		<cfset setFormFields(formFields) />
		<cfset runValidations() />
		
		<cfreturn this />
	</cffunction>
	
	<cffunction name="runValidations" access="public" output="false">
		<cfset var returnValue = "" />
		<cfset var i = 0 />
		<cfset var l = 0 />
		<cfset var v = 0 />
		<cfset var x = 0 />
		<cfset var y = 0 />
		<cfif structKeyExists(variables.instance,"formFields")>
			<cfsavecontent variable="returnValue">
			$().ready(function(){
			
				$("#formName").validate({
				<cfoutput>
					rules:{
						<cfloop from="1" to="#arrayLen(getFormFields())#" index="i">
							<cfif arrayLen(getFormFields()[i].validations)>
								#getFormFields()[i].fieldName#: {
								<cfloop from="1" to="#arrayLen(getFormFields()[i].validations)#" index="v">
									<cfif len(trim(getFormFields()[i].validations[v].ruleVal)) and listLen(getFormFields()[i].validations[v].ruleVal) EQ 1>									
									#getFormFields()[i].validations[v].rule#: #getFormFields()[i].validations[v].ruleVal#
									<cfelseif len(trim(getFormFields()[i].validations[v].ruleVal)) and listLen(getFormFields()[i].validations[v].ruleVal) GT 1>
									#getFormFields()[i].validations[v].rule#: [#getFormFields()[i].validations[v].ruleVal#]
									<cfelse>
									#getFormFields()[i].validations[v].rule#: "true"
									</cfif>
									<cfif arrayLen(getFormFields()[i].validations) GT 1 AND v LT arrayLen(getFormFields()[i].validations)>,</cfif>
								</cfloop>
								}
								<cfif i LT arrayLen(getFormFields())>,</cfif>
							</cfif>
						</cfloop>
					},
					messages:{
						<cfloop from="1" to="#arrayLen(getFormFields())#" index="x">
						<cfif arrayLen(getFormFields()[x].validations)>
								#getFormFields()[x].fieldName#: 
								<cfif arrayLen(getFormFields()[x].validations) EQ 1>
									<cfif len(trim(getFormFields()[x].validation[1].ruleMsg))>
										"#getFormFields()[x].validation[1].ruleMsg#"
									<cfelse>
										<cfswitch expression="#getFormFields()[x].validation[1].rule#">
											<cfcase value="required">
										"#jsRequiredMsg(getFormFields()[1])#"
											</cfcase>
											<cfcase value="numeric">
										"#jsNumericMsg(getFormFields()[1])#"
											</cfcase>
										</cfswitch>
									</cfif>
								<cfelse>
								{ 
								<cfloop from="1" to="#arrayLen(getFormFields())#" index="y">
								#getFormFields()[x].validations[y].rule#: 
									<cfif len(trim(getFormFields()[x].validations[y].ruleMsg))>
									"#getFormFields()[x].validations[y].ruleMsg#"
									<cfelse>
									<cfswitch expression="#getFormFields()[x].validations[y].rule#">
										<cfcase value="required">
									"#jsRequiredMsg(getFormFields()[x])#"
										</cfcase>
										<cfcase value="numeric">
									"#jsNumericMsg(getFormFields()[x])#"
										</cfcase>
									</cfswitch>
									</cfif>
								</cfloop>
								}
								</cfif>
							</cfif>
							</cfloop>
					}
				});
				</cfoutput>
			});
			
			</cfsavecontent>
			<cfdump var="#returnValue#">
			<cfabort>
		<cfelse>
			<cfreturn "Please initialize the validations using validationUtils.init(formFieldLocation)">
		</cfif>	
	</cffunction>
		

	<cffunction name="jsNumericMsg" access="public" returnType="string">
		<cfargument name="stField" type="struct" required="true" />		
		<cfreturn "The value entered for #arguments.fieldName# is not a valid number." />
	</cffunction>
	
	<cffunction name="ssDependency" access="public" returnType="string">
	
	</cffunction>
	<cffunction name="ssEmail" access="public" returnType="string">
	
	</cffunction>

	<cffunction name="ssDate" access="public" returnType="string">
	
	</cffunction>
	
	
	
	<cffunction name="setFormFields" access="public" returntype="void" output="false">
		<cfargument name="formFields" type="array" required="true" />
		<cfset variables.instance.formFields = arguments.formFields />		
	</cffunction>
	
	<cffunction name="getFormFields" access="public" returntype="array" output="false">
		<cfreturn variables.instance.formFields />
	</cffunction>
	
	<cffunction name="arrayOfStructsFind" access="private" returntype="numeric" output="false">
		<cfargument name="arrayToSearch" type="array" required="true" />
		<cfargument name="keyToSearch" type="string" required="true" />
		<cfargument name="valueToFind" type="string" required="true" />
		
		<cfset var result = 0 />
		<cfset var i = 0 />
		<cfset var key = "" />
		
		<cfloop from="1" to="#arrayLen(arguments.arrayToSearch)#" index="i">
			<cfif compareNoCase(arguments.arrayToSearch[i][arguments.keyToSearch],arguments.valueToFind) EQ 0>
				<cfreturn i>
			</cfif>
		</cfloop>
		
		<cfreturn 0 />
	</cffunction>
</cfcomponent>

